package com.self.liu.main;

import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.self.liu.xp.thrift.service.ExecuteService;

public class ThriftClient {

    public static ExecuteService.Client client = null;

    private static TTransport transport = null;

    public static int num = 0;

    private synchronized static ExecuteService.Client getClient() {
        if (client == null) {
            transport = new TSocket("10.144.25.142", 7777);
            System.out.println("creat" + client);
            try {
                transport.open();
            } catch (TTransportException e) {
                e.printStackTrace();
            }
            // 设置传输协议为 TCompactProtocol  
            TProtocol protocol = new TCompactProtocol(transport);
            client = new ExecuteService.Client(protocol);
        }
        return client;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.print("对象回收：共" + num);
        super.finalize();
    }

    public synchronized static String execute(String reqJson) {
        num++;
        return getClient().execute(reqJson);
    }

}
